/*
 * @Author: your name
 * @Date: 2021-06-26 23:04:50
 * @LastEditTime: 2021-06-26 23:31:37
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \ttstar\assets\scripts\libs\Global.js
 */

/**
 * 注入装饰器
 * @param {*} path 
 * @param {*} visible 
 * @returns 
 */
window.uiInject = function (path, visible = true) {
    return function (target, attr, descriptor) {
        // console.log(path);
        var injects = target["_injects"];
        if (injects == null) {
            injects = {};
            target["_injects"] = injects;
        }
        injects[attr] = [attr, path, visible];
    };
}

/**
 * 绑定装饰器
 * @param {*} resPath 
 * @param {*} objName 
 * @returns 
 */
window.uiBind = function (resPath, objName, resDeps) {
    return function (target) {
        target["resPath"] = resPath;
        target["objName"] = objName;
        target["resDeps"] = resDeps;
    };
}

/**
 * 挂接装饰器
 * @param {*} path 
 * @param {*} classType 
 * @returns 
 */
window.uiAttach = function (path, classType, args) {
    return function (target, attr, descriptor) {
        var attachs = target["_attachs"];
        if (attachs == null) {
            attachs = {};
            target["_attachs"] = attachs;
        }
        attachs[attr] = [attr, path, classType, args];
    };
}

// /**
//  * 派发事件
//  * @param name 
//  */
// window.uiDispatch = function (name) {
//     return function (target, attr, descriptor) {

//     };
// }

/**
 * 添加事件
 * @param name 
 */
window.uiListener = function (name) {
    return function (target, attr, descriptor) {
        var events = target["_listeners"];
        if (events == null) {
            events = {};
            target["_listeners"] = events;
        }
        events[name] = attr;
    };
}

/**
 * 唯一码标记
 * @param {*} name 
 * @returns 
 */
window.uuid_sign = function (name) {
    return function (target) {
        target["uuid_sign"] = name;
    };
}

var ONLY_CALL_LIST = [];

/**
 * 锁定函数
 * @param {*} handler 
 * @param {*} target 
 */
window.lock_func = function (handler, target) {
    for (var i = 0; i < ONLY_CALL_LIST.length; ++i) {
        var temp = ONLY_CALL_LIST[i];
        if (temp.handler == handler && temp.target == target) {
            return true;
        }
    }
    ONLY_CALL_LIST.push({ handler, target });
    return false;
}

/**
 * 解锁函数
 * @param {*} handler 
 * @param {*} target 
 */
window.unlock_func = function (handler, target) {
    for (var i = 0; i < ONLY_CALL_LIST.length; ++i) {
        var temp = ONLY_CALL_LIST[i];
        if (temp.handler == handler && temp.target == target) {
            ONLY_CALL_LIST.splice(i, 1);
            break
        }
    }
}

/**
 * 解锁函数
 * @param {*} handler 
 * @param {*} target 
 */
window.once_call = async function (handler, target, ...args) {
    if (lock_func(handler, target))
        return null;
    var res = await handler.call(target, ...args);
    unlock_func(handler, target)
    return res;
}

/**
 * 异步函数执行是否完成
 * @param {*} handler 
 * @param {*} target 
 */
window.is_call_finish = function (handler, target) {
    for (var i = 0; i < ONLY_CALL_LIST.length; ++i) {
        var temp = ONLY_CALL_LIST[i];
        if (temp.handler == handler && temp.target == target) {
            return false;
        }
    }
    return true;
}